package scr 
{
	/**
	 * ...
	 * @author a.orleansky
	 */
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import img.ScalePlaneNoText;
	import ru.gosoplata.model.TmpModel;
	import uicontrols.ButtonBase;
	
	public class ScreenFinishPay extends ScreenBase  implements IScreen
	{
		
		private var inputtxt:TextField;
		private var inputtxt2:TextField;
		private var pic_count:Number;
		private	var btn_next:ButtonBase = new ButtonBase("next", 0x00FF00);
		
		private var pics_info:Array = new Array(new Array("plane_big", TmpModel.IMG_FOLDER+"/blagorim_za_ispolzovanie/images/plane_big_platezi.png"),
												new Array("plane_tel", TmpModel.IMG_FOLDER+"/blagorim_za_ispolzovanie/images/plane_tel.png"),
												new Array("plane_money", TmpModel.IMG_FOLDER+"/blagorim_za_ispolzovanie/images/plane_money.png"),
												new Array("text_blag", TmpModel.IMG_FOLDER+"/blagorim_za_ispolzovanie/images/text_blag.png"),
												new Array("text_schet", TmpModel.IMG_FOLDER+"/blagorim_za_ispolzovanie/images/text_schet.png"));
		
		private var curr_pic:Array;
		
		public function ScreenFinishPay() 
		{
			super("finish_pay");
		}
		
		override protected function init(e:Event):void 
		{
			super.init(e);
			
			pic_count = 0;
			btn_next.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_next.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			addChild(btn_next);
			pic_count++
			pics_info = pics_info.reverse();
			loadPictures();
		}
		
		override public function reDraw():void
		{
			inputtxt.text = TmpModel.payment.accauntnumber;
			inputtxt2.text = TmpModel.payment.forEnrollment + " руб";
		}		
		
		private function loadPictures():void {
			if(pics_info.length > 0){
				curr_pic = (pics_info.pop() as Array);
				
				var url:String = TmpModel.IMG_FOLDER+"/first_page/images/flag.png";
				url = curr_pic[1];
				var req:URLRequest = new URLRequest(url);
				var loader:Loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void { } );
				loader.load(req);
			}
		}
		
		private function onLoadComplete(e:Event):void 
		{
			var ldr:Loader = Loader(e.target.loader);
			var image:Bitmap = Bitmap(ldr.content);
			placeImage(image);
		}
		
		private var plane_tel_x:Number;
		private var plane_tel_y:Number;
		private var plane_tel_wd:Number;
		private var text_y:Number;
		//private var logo:Bitmap;
		private var scale_plane:ScalePlaneNoText;
		
		public function placeImage(image:Bitmap):void {
			switch(curr_pic[0]) {
				case "plane_big":
					image.x = stage.stageWidth/2-image.width/2;
					image.y = 250;
					addChild(image);
					break;
				case "plane_tel":
					image.x = 190;
					image.y = 370;
					plane_tel_x = image.x;
					plane_tel_y = image.y;
					plane_tel_wd = 700;

					scale_plane = new ScalePlaneNoText(700);
					scale_plane.x = plane_tel_x;
					scale_plane.y = plane_tel_y;
					addChild(scale_plane);
					
					inputtxt = new TextField();
					var fmt:TextFormat = TmpModel.getTextFormat(44, 0xFFFFFF,true);
					fmt.letterSpacing = 5;
					//var userphone:String = UserInfo.getInstance().getPhone();
					inputtxt.embedFonts = true;
					inputtxt.x = plane_tel_x;
					inputtxt.y = plane_tel_y+7;
					inputtxt.width = plane_tel_wd;
					inputtxt.autoSize = TextFieldAutoSize.CENTER;
					inputtxt.defaultTextFormat = fmt;
					inputtxt.antiAliasType = AntiAliasType.ADVANCED;
					//inputtxt.text = "8(" + userphone.substr(0, 3) + ")" + userphone.substr(3, 3) + "-" + userphone.substr(6, 2) + "-" + userphone.substr(8);
					
					addChild(inputtxt);
					break;
				case "plane_money":
					image.x = plane_tel_x+7;
					image.y = plane_tel_y+image.height+10;
					addChild(image);
					inputtxt2 = new TextField();
					var fmt2:TextFormat = TmpModel.getTextFormat(44, 0xFFFFFF, true);
					fmt2.letterSpacing = 5;
					inputtxt2.embedFonts = true;
					inputtxt2.x = image.x+20;
					inputtxt2.y = image.y+10;
					inputtxt2.width = image.width-20;
					inputtxt2.autoSize = TextFieldAutoSize.CENTER;
					inputtxt2.defaultTextFormat = fmt2;
					inputtxt2.antiAliasType = AntiAliasType.ADVANCED;
					inputtxt2.text = "0.000";
					
					addChild(inputtxt2);
					break;
				case "text_blag":
					image.x = 200;
					image.y = 560;
					text_y = image.y;
					addChild(image);
					break;
				case "text_schet":
					image.x = 200;
					image.y = 330;
					text_y = 500;
					addChild(image);
					break;
				default:
					image.x = stage.stageWidth/2-image.width/2;
					image.y = stage.stageHeight - image.height;
					addChild(image);
				break;
			}
			loadPictures();
		}
		
		private function allPicLoaded(e:Event):void 
		{
			pic_count--;
			if (pic_count == 0) {
				
				btn_next.x = stage.stageWidth - (btn_next.width + 20)
				btn_next.y = stage.stageHeight - (btn_next.height + 20);
			}
		}
		
		private function pressBtn(e:Event):void 
		{
			switch(e.target.name) {
				case "next":
					TmpModel.tmpGlobalController.controllerFinishPay();
					break;
			}
		}
		
	}

}